package com.dchh.android.suverynew.adapter;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dchh.android.suverynew.R;
import com.dchh.android.suverynew.act.TaskMainActivity;
import com.dhcc.android.base.common.bean.Task;
import com.dhcc.android.base.utils.StringUtil;

import java.util.List;

/**
 * 已下载任务 列表 操作 适配器
 *
 * @author zhoudan
 * @date 2015年5月19日
 */
public class TaskOperateAdapter extends BaseAdapter {

    private Activity activity;
    private List<Task> list;
    private List<String> counts;
    private TaskMainActivity.StartResearchListener listener;

    public TaskOperateAdapter(Activity activity, List<Task> list, List<String> counts) {
        this.activity = activity;
        this.list = list;
        this.counts = counts;
    }

    public TaskOperateAdapter(Activity activity, List<Task> list) {
        this.activity = activity;
        this.list = list;
    }


    public void setListener(TaskMainActivity.StartResearchListener listener) {
        this.listener = listener;
    }


    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Task getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View row = convertView;
        ViewHolder holder;
        if (row == null) {
            holder = new ViewHolder();
            row = LayoutInflater.from(activity).inflate(R.layout.task_down_list_item, null);
            holder.id = (TextView) row.findViewById(R.id.task_id);
            holder.county = (TextView) row.findViewById(R.id.task_county);
            holder.name = (TextView) row.findViewById(R.id.task_name);
            holder.item = (LinearLayout) row.findViewById(R.id.task_info_layout);
            row.setTag(holder);
        } else {
            holder = (ViewHolder) row.getTag();
        }

        final Task task = getItem(position);

        holder.county.setText(task.getTitle());
        holder.name.setText(task.getDescription());

        holder.item.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                /**
                 * 跳转到层级选择界面
                 *
                 */
                if (StringUtil.isFastDoubleClick(500)) {
                    return;
                }
                if (listener != null) {
                    listener.onStart(position);
                }
            }
        });
        return row;
    }


    public final class ViewHolder {
        public TextView id;
        public TextView county;
        public TextView name;
        public LinearLayout item;
    }
}
